<?php

namespace App\Http\Controllers\Admin;

use App\Http\Resources\CarCollection;
use App\Models\Car;
use Illuminate\Http\Request;

class CarsController extends AdminController
{

    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth:api');
    }

    public function carsList(Request $request, Car $model)
    {
        $per_page = $request->get('per_page', 10);

        $search_data = json_decode($request->get('search_data'), true);

        $keyword = isset_and_not_empty($search_data, 'keyword');
        if ($keyword) {
            $model = $model->where('brand', 'like', "%{$keyword}%")
                ->orWhere('frame_no', 'like', "%{$keyword}%")
                ->orWhere('series', 'like', "%{$keyword}%");
        }

        $first_at = date_isset_and_not_empty($search_data, 'first_at');
        if ($first_at) {
            $model = $model->columnBetween("first_at", $first_at);
        }

        $filed_at = date_isset_and_not_empty($search_data, 'filed_at');
        if ($filed_at) {
            $model = $model->columnBetween("filed_at", $filed_at);
        }

        $status = isset_and_not_empty($search_data, 'status');
        if ($status) {
            $model = $model->where("status", $status);
        }

        $order_by = isset_and_not_empty($search_data, 'order_by');
        if ($order_by) {
            $order_by = explode(',', $order_by);
            $model = $model->orderBy($order_by[0], $order_by[1]);
        }
        return new CarCollection($model->paginate($per_page));
    }

    // 判断车架号是否存在
    public function existFrameNo($frame_no){
        $result = Car::where('frame_no', $frame_no)->exists();
        return $this->success($result);
    }

    public function show(Car $model)
    {
        return $this->success($model->makeVisible('appraisal_report'));
    }

    public function addCar(Request $request, Car $model)
    {
        $request_data = $request->all();
        if($request_data['diagnosis_report'] == []){
            return $this->failed('必须上传诊断图片');
        }
        // 上传诊断多图处理
        foreach ($request_data['diagnosis_report'] as $k => $v) {
            if (isset($v['attachment_id'])) {
                $request_data['diagnosis_report'][$k] = $v['attachment_id'];
            }
        }
        // 如果有上传鉴定多图，即进行处理
        if($request_data['appraisal_report'] != []) {
            foreach ($request_data['appraisal_report'] as $k => $v) {
                if (isset($v['attachment_id'])) {
                    $request_data['appraisal_report'][$k] = $v['attachment_id'];
                }
            }
        }
        $res = $model->saveAction($request_data);
        if ($res) return $this->message('操作成功');
        return $this->failed('内部错误');
    }

    public function updateStatus(Car $model, Request $request)
    {
        $request_data["status"] = $request->status;

        $res = $model->updateStatusAction($request_data);

        if ($res['status'] === true) {
            return $this->message($res['message']);
        }
        return $this->failed($res['message']);
    }

    public function update(Car $model, Request $request)
    {
        $request_data = $request->all();
        if($request_data['diagnosis_report'] == []){
            return $this->failed('必须上传诊断图片');
        }
        // 上传诊断多图处理
        foreach ($request_data['diagnosis_report'] as $k => $v) {
            if (isset($v['attachment_id'])) {
                $request_data['diagnosis_report'][$k] = $v['attachment_id'];
            }
        }
        // 如果有上传鉴定多图，即进行处理
        if($request_data['appraisal_report'] != []) {
            foreach ($request_data['appraisal_report'] as $k => $v) {
                if (isset($v['attachment_id'])) {
                    $request_data['appraisal_report'][$k] = $v['attachment_id'];
                }
            }
        }
        $res = $model->updateAction($request_data);

        if ($res['status'] === true) {
            return $this->message($res['message']);
        }
        return $this->failed($res['message']);
    }

    public function destroy(Car $model)
    {
        $rest_destroy = $model->destroyAction();
        if ($rest_destroy['status'] === true) return $this->message($rest_destroy['message']);
        return $this->failed($rest_destroy['message'], 500);
    }
}
